package com.ssmTest.controller;


import com.ssmTest.model.SystemSetup;
import com.ssmTest.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpSession;
import java.text.ParseException;


@Controller
public class AdminController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMachineUserInformationService iMachineUserInformationService;
    @Autowired
    private IUserInformationService iUserInformationService;
    @Autowired
    private ISystemSetupService iSystemSetupService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private DateTimeService dateTimeService;

    //主页
    @RequestMapping(value = "/")
    public String inits() {
        return "login";
    }

    //登录
    @RequestMapping(value = "/login.do")
    public String login(HttpSession session, String no, String password, ModelMap modelMap) {
        int type;
        type = userService.userLogins(session, no, password, modelMap);
        //0管理员 1用户登录成功 2密码错误  3用户不存在
        if (type == 1) {
            return "adminMain";   //用户主界面
        } else if (type == 0) {
            return "index";   //管理员界面
        } else {
            modelMap.addAttribute("msg", "error");
            return "login";   //登录界面
        }
    }

    //退出登录
    @RequestMapping(value = "/loginout.do")
    public String loginout(HttpSession session) {
        session.removeAttribute("id");
        session.removeAttribute("phone");
        return "login";
    }

    //welcome页面
    @RequestMapping(value = "/welcome.do")
    public String welcome(ModelMap modelMap) throws ParseException {
        String date = dateTimeService.getTime("yyyy.mm.dd");
        modelMap.addAttribute("data1", userService.numberOfUser(1)); //待审核用户

        modelMap.addAttribute("data2", userService.numberOfUser(date, date));//今天注册用户数
        modelMap.addAttribute("data3", userService.numberOfUser(dateTimeService.addday(date, -7), date));//一周内注册用户数
        modelMap.addAttribute("data4", userService.numberOfUser(dateTimeService.addday(date, -7), date));//一月内注册用户数

        modelMap.addAttribute("data5", iMachineUserInformationService.numberOfInformation(0));//待审核机手发布信息
        modelMap.addAttribute("data6", iUserInformationService.numberOfInformation(0));//待审核普通用户发布信息

        modelMap.addAttribute("data7", iOrderService.numberOfOrder(date, date));//今天生成订单数
        modelMap.addAttribute("data8", iOrderService.numberOfOrder(dateTimeService.addday(date, -7), date));//一周内生成订单数
        modelMap.addAttribute("data9", iOrderService.numberOfOrder(dateTimeService.addday(date, -7), date));//一月内生成订单数
        return "welcome";
    }


    //返回系统设置界面
    @RequestMapping(value = "/systemsetpage")
    public String systemsetpage(ModelMap modelMap) {
        SystemSetup systemSetup = iSystemSetupService.getSystem();
        System.out.println(systemSetup.getId());
        modelMap.addAttribute("systemdate", systemSetup);
        return "systemSet";
    }

    //设置新的系统数据
    @RequestMapping(value = "/setSystem")
    public String setSystem(ModelMap modelMap, double royalty, double royalty_of_user) {
        SystemSetup systemSetup = iSystemSetupService.getSystem();
        if (royalty != 0) {
            systemSetup.setRoyalty(royalty);
        }
        if (royalty_of_user != 0) {
            systemSetup.setRoyalty_of_user(royalty_of_user);
        }
        System.out.println(systemSetup.getRoyalty() + "   " + systemSetup.getRoyalty_of_user());
        iSystemSetupService.updateSystem(systemSetup);
        modelMap.addAttribute("systemdate", systemSetup);
        return "success";
    }
}
